import 'dart:convert';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/pojo/Enums.dart';
import 'package:im_app/im/service/ImCollectService.dart';
import 'package:im_app/im/service/LoginService.dart';
import 'package:im_app/im/socket/dto/business/Login.dart';
import 'package:im_app/im/socket/dto/business/PullData2.dart';
import 'package:im_app/im/socket/service/RpcPullDataService.dart';
import 'package:im_app/im/socket/service/RpcSendDataService.dart';
import 'package:logger/logger.dart';

import '../BaseImTest.dart';

void main() {
  test("test pull configs", () async {
    var imWebSocket = await BaseImTest.initWebSocket();
    var dataSource = await BaseImTest.initDb();

    var logger = Logger();
    logger.i("websocket run");

    LoginReq loginReq = LoginReq(userId: "朱元璋", password: "123456", machineCode: "demo",
        tenantCode: "0002", terminalType: "android");
    logger.i("login json: " + loginReq.toJson().toString());
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);
    logger.i("login success result : " + jsonEncode(loginResp));

    var rpcPullDataService = new RpcPullDataService();

    var configPullReq = new ConfigPullReq();
    var configItemList = await rpcPullDataService.pullConfigList(configPullReq);
    // logger.i("configData: " + jsonEncode(configItemList));

    var duration = const Duration(seconds: 10);
    logger.i('Start sleeping');
    sleep(duration);
    dataSource?.close();
    // imWebSocket?.close();
  }, timeout: Timeout.factor(3));

  test("test pull modules", () async {
    var imWebSocket = await BaseImTest.initWebSocket();
    var dataSource = await BaseImTest.initDb();
    var logger = Logger();
    logger.i("websocket run");

    LoginReq loginReq = LoginReq(userId: "朱元璋", password: "123456", machineCode: "demo",
        tenantCode: "0002", terminalType: "android");
    logger.i("login json: " + loginReq.toJson().toString());
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);
    logger.i("login success result : " + jsonEncode(loginResp));

    var rpcPullDataService = new RpcPullDataService();

    var modulePullReq = new ModulePullReq();
    var moduleItemList = await rpcPullDataService.pullModuleItemList(modulePullReq);
    // logger.i("configData: " + jsonEncode(configItemList));

    var duration = const Duration(seconds: 10);
    logger.i('Start sleeping');
    sleep(duration);
    // imWebSocket?.close();
    dataSource.close();
  }, timeout: Timeout.factor(3));

  test("test pull collects", () async {
    var imWebSocket = await BaseImTest.initWebSocket();
    var dataSource = await BaseImTest.initDb();
    var logger = Logger();
    logger.i("websocket run");

    LoginReq loginReq = LoginReq(userId: "zhugeliang", password: "123456", machineCode: "demo",
        tenantCode: "0002", terminalType: "android");
    logger.i("login json: " + loginReq.toJson().toString());
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);
    logger.i("login success result : " + jsonEncode(loginResp));

    var rpcPullDataService = RpcPullDataService();

    var collectPullReq = CollectPullReq();
    var collectItemList = await rpcPullDataService.pullCollects(collectPullReq);

    var collectService = ImCollectService();
    collectService.saveOrUpdates('zhugeliang', collectItemList.collectInfoItems);
    // logger.i("configData: " + jsonEncode(collectItemList));

    var duration = const Duration(seconds: 10);
    logger.i('Start sleeping');
    sleep(duration);
    dataSource.close();
    // imWebSocket?.close();
  }, timeout: Timeout.factor(3));
}

